package com.example.controller;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.activiti.TaskInfo;
import com.example.entity.dto.FileUpload.FileDto;
import com.example.entity.dto.FileUpload.FileListDto;
import com.example.entity.dto.PurchaseOrder.PurchaseDetailDto;
import com.example.entity.dto.PurchaseOrder.PurtcListAddDto;
import com.example.entity.dto.PurchaseOrder.PurtcListDto;
import com.example.entity.dto.TCInfo;
import com.example.entity.dto.User.MyUser;
import com.example.entity.hz.HttpResult;
import com.example.entity.hz.ShenHe;
import com.example.entity.vo.PurchaseOrder.PurchaseDetailVo;
import com.example.entity.vo.PurchaseOrder.PurtcListVo;
import com.example.entity.vo.PurchaseOrder.PurtdListVo;
import com.example.entity.yifei.*;
import com.example.entity.ymlConfig.YmlConfig;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.service.FileService;
import com.example.service.PurtcService;
import com.example.service.impl.ActFlowCommService;
import com.example.untils.HttpClientUtil;
import com.example.untils.SecurityUtils;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/api/purtc")
@Api(tags = "采购单相关接口")
@Slf4j
public class PurtcController {

    @Resource
    private PurtcService purtcService;

    @Resource
    private PurtcMapper purtcMapper;

    @Resource
    private PurtdMapper purtdMapper;

    @Autowired
    private PurcdMapper purcdMapper;

    @Autowired
    private CgdzjbMapper cgdzjbMapper;   // 采购单中间表

    @Autowired
    private ActFlowCommService actFlowCommService;

    @Resource
    private AttachMapper attachMapper;

    @Autowired
    private FileService fileService;

    @Resource
    private InvmdMapper invmdMapper;
    @Resource
    private InvmbMapper invmbMapper;

    
    @ApiOperation("通过品号查询到品号单位进行换算")
    @GetMapping("/queryMD")
    public Result<List<Invmd>> queryMD(String md001){
        List<Invmd> invmds = this.invmdMapper.selectList(new LambdaQueryWrapper<Invmd>().eq(Invmd::getMD001, md001));
        Invmb invmb = this.invmbMapper.selectOne(new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, md001));
        Invmd invmd = new Invmd();
        invmd.setMD001(invmb.getMb001());
        invmd.setMD002(invmb.getMb004());
        invmd.setMD003(new BigDecimal(1));
        invmd.setMD004(new BigDecimal(1));
        invmd.setDivision(0);
        invmds.add(invmd);
        return Result.success(invmds);
    }

    @Log("采购单新增接口")
    @ApiOperation("采购单新增接口")
    @PostMapping("/add")
    public Result add(@RequestBody PurtcListAddDto purtcListAddDto) throws ExecutionException, InterruptedException {
//        System.out.println(purtcListAddDto);
        CompletableFuture<Result> resultCompletableFuture = purtcService.add2(purtcListAddDto);
        // 等待异步任务完成并获取结果
        Result result = resultCompletableFuture.get(); // 这里会阻塞直到异步任务完成
        // 返回结果给前端
        return result;
    }

    @Log("审核采购单 单表")
    @ApiOperation("审核采购单 单表")
    @PostMapping("/checkPurtc")
    public Result checkPurtc(@RequestParam(defaultValue = "") String TC001,
                             @RequestParam(defaultValue = "") String TC002) {
        if(StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        if(purtc == null) {
            return Result.error("单据不存在!");
        }
        purtc.setTc014("Y");
        purtcMapper.update(purtc, new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        Purtd purtd = new Purtd();
        purtd.setTd018("Y");
        purtdMapper.update(purtd, new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd001, TC001).eq(Purtd::getTd002, TC002));
        return Result.success("审核采购单成功");
    }

    @Log("删除采购单 单表")
    @ApiOperation("删除采购单 单表")
    @PostMapping("/delPurtc")
    public Result delPurtc(@RequestParam(defaultValue = "") String TC001,
                           @RequestParam(defaultValue = "") String TC002) {
        if(StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        if(purtc == null) {
            return Result.error("单据不存在!");
        }
        if("Y".equals(purtc.getTc014())) {
            return Result.error("单据已审核, 不能删除");
        }else if("V".equals(purtc.getTc014())) {
            return Result.error("单据已作废, 不能删除");
        }
        if("1".equals(purtc.getTc030())) {
            return Result.error("单据在审核中,不能删除");
        }else if("3".equals(purtc.getTc030())) {
            return Result.error("单据已审核,不能删除");
        }
        purtcService.delPurtc(TC001, TC002);
        List<Attach> list = attachMapper.selectList(new LambdaQueryWrapper<Attach>().eq(Attach::getParent, "PURTCR6")
                .eq(Attach::getKeyvalues, TC001 + "||" + TC002));
        for(Attach attach : list) {
            System.out.println("删除：" + attach.getDocid());
            // 找不到的就是删除
            attachMapper.delete(new LambdaQueryWrapper<Attach>().eq(Attach::getDocid, attach.getDocid()));
        }
        return Result.success("删除采购单成功");
    }

//    @PreAuthorize("@ss.hasPermi('purtc:list')")
    @ApiOperation("采购单列表")
    @PostMapping("/list")
    public Result<PageInfo<List<PurtcListVo>>> list(@RequestParam(defaultValue = "0") int current,
                                 @RequestParam(defaultValue = "10") int size,
                                 @RequestBody PurtcListDto purtcListDto) {
//        Page<PurtcListVo> page = new Page<>(pageNum, pageSize);
//        page.setOptimizeCountSql(false);
        PageHelper.startPage(current, size, "PURTC.CREATE_DATE DESC");
        if(StringUtils.isNotEmpty(purtcListDto.getTD006())) {
            purtcListDto.setTD006(purtcListDto.getTD006().replaceAll("([\\[\\]*+_%^])", "/$1"));
        }
        List<PurtcListVo> list = purtcMapper.getList(purtcListDto);
        String formKey="purtc";
        list.forEach(purtcListVo -> {
            String businessKey=formKey+":"+purtcListVo.getTC001().trim()+":"+purtcListVo.getTC002();
            String curStatus = actFlowCommService.getCurStatus(businessKey, formKey);
            purtcListVo.setCurStatus(curStatus);
        });
        PageInfo<List<PurtcListVo>> pageInfo = new PageInfo(list);

        return Result.success(pageInfo);
    }

    @ApiOperation("采购单详情")
    @PostMapping("/purchaseDetail")
    public Result<PurchaseDetailVo> purchaseDetail(@RequestParam(defaultValue = "") String TC001,
                                                   @RequestParam(defaultValue = "") String TC002) {
        if (TC001.equals("") || TC002.equals("")) {
            return Result.error("单别或单号为空!");
        }
        PurchaseDetailVo res = purtcService.purchaseDetail(TC001, TC002);
        if(res == null) {
            return Result.error("该单据不存在!");
        }
        res.setPurtdList(purtdMapper.getPurtdListByTC001TC002(TC001, TC002));
        List<PurtdListVo> purtdList = res.getPurtdList();
        Set<String> reqUserName = new HashSet<>();
        Set<String> reqDeptName = new HashSet<>();
        for (PurtdListVo listVo : purtdList) {
            Double td010Double = this.purtdMapper.getTd010Double(res.getTC004(), listVo.getTD004());
            if(td010Double!=null){
                listVo.setNuclearPriceLs(td010Double);
                if (listVo.getTD010()>td010Double){
                    listVo.setHistoricalunitprice(2);
                }else if (listVo.getTD010().equals(td010Double)){
                    listVo.setHistoricalunitprice(1);
                }else {
                    listVo.setHistoricalunitprice(0);
                }
            }
            // 中间表， 按子序号排序
            List<Cgdzjb> list = cgdzjbMapper.getListByTD(TC001, TC002, listVo.getTD003());
            StringBuffer sb = new StringBuffer();
            // TB001-TB002-TB003,TB001-TB002-TB003
            for (int idx = 0; idx < list.size(); idx++) {
                sb.append(list.get(idx).getTb001().trim() + "-" + list.get(idx).getTb002().trim() + "-" + list.get(idx).getTb003().trim());
                if (idx != list.size() - 1) sb.append(",");
            }
            listVo.setPurtbSummary(sb.toString());
            String udf04 = listVo.getUDF04(); //请购部门名称
            if(udf04 != null) {
                String[] strings = udf04.split(",");
                for(String s : strings) {
                    reqDeptName.add(s);
                }
            }
            String udf06 = listVo.getUDF06(); //请购人员名称
            if(udf06 != null) {
                String[] strings = udf06.split(",");
                for(String s : strings) {
                    reqUserName.add(s);
                }
            }
        }
        StringBuffer reqUser = new StringBuffer();
        if(reqUserName.size() > 0) {
            for(String s : reqUserName) {
                reqUser.append(s).append(",");
            }
            reqUser.deleteCharAt(reqUser.length() - 1);
        }
        StringBuffer reqDept = new StringBuffer();
        if(reqDeptName.size() > 0) {
            for(String s : reqDeptName) {
                reqDept.append(s).append(",");
            }
            reqDept.deleteCharAt(reqDept.length() - 1);
        }

        List<Attach> fileList = fileService.fileList(new FileListDto(TC001, TC002, "PURTCR6", YmlConfig.getYFCOMPANY()));
        List<String> urls = new ArrayList<>();
        for(int i = 0; i < fileList.size(); i++) {
            String docId = fileList.get(i).getDocid();
            String fileName = fileList.get(i).getFilename();
            String url = "/files/" + docId.substring(3, 6) + "000" +
                    "/" + docId + "/" + fileName;
            urls.add(url);
        }
        res.setUrls(urls);
        res.setME002(reqDept.toString());
        res.setPurchaseUserName(reqUser.toString());
        return Result.success(res);
    }

    @Log("增删改采购单单身")
    @ApiOperation("增删改采购单单身")
    @DSTransactional
    @PostMapping("/update")
    public Result update(@RequestBody PurchaseDetailDto purchaseDetailDto) {
        if(StringUtils.isEmpty(purchaseDetailDto.getTC001()) || StringUtils.isEmpty(purchaseDetailDto.getTC002())) {
            return Result.error("单别或单号为空");
        }
        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, purchaseDetailDto.getTC001())
                .eq(Purtc::getTc002, purchaseDetailDto.getTC002()));
        if(purtc == null) {
            return Result.error("单据已删除或不存在!");
        }
        if("Y".equals(purtc.getTc014())) {
            return Result.error("单据已审核, 不能修改");
        }else if("V".equals(purtc.getTc014())) {
            return Result.error("单据已作废, 不能修改");
        }
        if("1".equals(purtc.getTc030())) {
            return Result.error("单据在审核中,不能修改");
        }else if("3".equals(purtc.getTc030())) {
            return Result.error("单据已审核,不能修改");
        }
        purtcService.delPurtc(purchaseDetailDto.getTC001(), purchaseDetailDto.getTC002());
        purtc.setIdentifier(purchaseDetailDto.getIdentifier());
        purtcService.add3(purchaseDetailDto, purtc);
//        if(purchaseDetailDto.getFileList() != null && purchaseDetailDto.getFileList().size() > 0) {
            List<Attach> list = attachMapper.selectList(new LambdaQueryWrapper<Attach>().eq(Attach::getParent, "PURTCR6")
                    .eq(Attach::getKeyvalues, purchaseDetailDto.getTC001() + "||" + purchaseDetailDto.getTC002()));
            int idx = 0;
            for(Attach attach : list) {
                boolean f = false;
                idx = Math.max(idx, Integer.parseInt(attach.getSeqno()));
                if(purchaseDetailDto.getFileList() != null) {
                    for(FileDto fileDto : purchaseDetailDto.getFileList()) {
                        if(fileDto.getDocID().trim().equals(attach.getDocid().trim())) {
                            f = true;
                            break;
                        }
                    }
                    if(f) continue;
                }
                System.out.println("删除：" + attach.getDocid());
                // 找不到的就是删除
                attachMapper.delete(new LambdaQueryWrapper<Attach>().eq(Attach::getDocid, attach.getDocid()));
            }
            idx++;
            if(purchaseDetailDto.getFileList() != null) {
                for(FileDto fileDto : purchaseDetailDto.getFileList()) {
                    Boolean f = false;
                    for(Attach attach : list) {
                        if(fileDto.getDocID().trim().equals(attach.getDocid().trim())) {
                            f = true;
                            break;
                        }
                    }
                    if(f) continue;
                    // 找不到的就是新增
                    String addDate = TimeUtils.getNowTime().substring(0, 8);
                    // 获取当前时间
                    Date currentDate = new Date();
                    // 格式化日期时间
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                    String addTime = sdf.format(currentDate);
                    Attach attach = new Attach("PURTCR6", purchaseDetailDto.getTC001()+"||"+purchaseDetailDto.getTC002(),
                            null, purchaseDetailDto.getCreator(), null, String.format("%03d", idx),
                            null, fileDto.getDocID(), null, addDate, addTime, "TC001||TC002");
                    attachMapper.update(attach, new LambdaQueryWrapper<Attach>().eq(Attach::getDocid, fileDto.getDocID()));
                    idx++;
                }
            }
//        }
        return Result.success("修改成功");
    }

//    @Log("增删改采购单单身")
//    @ApiOperation("增删改采购单单身")
//    @PostMapping("/update")
//    public Result update(@RequestBody PurchaseDetailDto purchaseDetailDto) {
//        if (purchaseDetailDto.getOperateMethod().equals("") ||
//                purchaseDetailDto.getOperateMethod() == null) {
//            return Result.error("操作类型为空");
//        }
//        if(StringUtils.isEmpty(purchaseDetailDto.getTC001()) || StringUtils.isEmpty(purchaseDetailDto.getTC002())) {
//            Result.error("单别或单号为空");
//        }
//        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, purchaseDetailDto.getTC001())
//                .eq(Purtc::getTc002, purchaseDetailDto.getTC002()));
//        if(purtc == null) {
//            Result.error("单据不存在!");
//        }
//        if("Y".equals(purtc.getTc014())) {
//            Result.error("单据已审核, 不能修改");
//        }else if("V".equals(purtc.getTc014())) {
//            Result.error("单据已作废, 不能修改");
//        }
//        if("1".equals(purtc.getTc030())) {
//            return Result.error("单据在审核中,不能修改");
//        }else if("3".equals(purtc.getTc030())) {
//            return Result.error("单据已审核,不能修改");
//        }
//        LambdaQueryWrapper<Purcd> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Purcd::getCd010, purchaseDetailDto.getTC001())
//                .eq(Purcd::getCd011, purchaseDetailDto.getTC002());
//        Integer count = purcdMapper.selectCount(wrapper);
//        if (count > 0) {
//            throw new CustomException("500", "该单据已有下流单据不能直接修改");
//        }
//        if (purchaseDetailDto.getOperateMethod().equals("add") || purchaseDetailDto.getOperateMethod().equals("update")) {
//            return purtcService.addOrUpdatePurtd(purchaseDetailDto);
//        }
//        if (purchaseDetailDto.getOperateMethod().equals("delete")) {
//            return purtcService.delPurtd(purchaseDetailDto);
//        }
//        return Result.error("操作类型不存在");
//    }
    @Log("上报采购单")
    @ApiOperation("上报采购单")
    @PostMapping("/report")
    @DSTransactional
    public Result report(
             @RequestParam String TC001,@RequestParam String TC002,@RequestParam String leader
    ) {
        MyUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser==null){
            throw new CustomException("500","用户为空,请重新登录!");
        }
        String userName = loginUser.getUsername();
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(userName)){
            return Result.error("用户名为空");
        }
        String formkey = "purtc";
        String beanName = formkey + "ServiceImpl";
//关联业务 bussinessKey
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 +":"+ TC002;
        Boolean b=actFlowCommService.isOnly(bussinessKey,formkey);
        Boolean b2=purtcService.tc30IsN(TC001,TC002);
        ProcessInstance processInstance;
        if (b&&b2){
            processInstance= actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, userName, leader);
        }
         else {
             return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(userName);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(userName) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", userName, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报采购单成功");
    }
    @Log("批量上报采购单")
    @ApiOperation("批量上报采购单")
    @PostMapping("/reportlist")
    @DSTransactional
    public Result reportlist(
            @RequestBody List<TCInfo> tcInfoList
    ) {

        MyUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser==null){
            return Result.error("用户未登录,请退出重新登录!");
        }
        String userName = loginUser.getUsername();
        if (tcInfoList.isEmpty()) {
            return Result.error("单别或单号列表为空,或已全部上报,无需上报");
        }
        if (StringUtils.isEmpty(userName)){
            throw new BaseException("用户名为空");
        }
        String formkey = "purtc";
        String beanName = formkey + "ServiceImpl";
        tcInfoList.forEach(tcInfo -> {
            if (StringUtils.isEmpty(tcInfo.getTC001()) || StringUtils.isEmpty(tcInfo.getTC002())) {
                throw new BaseException("单别或单号为空");
            }
            String bussinessKey = formkey + ":" + tcInfo.getTC001() + ":" + tcInfo.getTC002();
            String id = tcInfo.getTC001() +":"+ tcInfo.getTC002();
            Boolean b=actFlowCommService.isOnly(bussinessKey,formkey);
            Boolean b2=purtcService.tc30IsN(tcInfo.getTC001(),tcInfo.getTC002());
            String leader = tcInfo.getLeader();
            ProcessInstance processInstance;
            if (b&&b2){
                processInstance= actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, userName,leader);
            }
            else {
                throw new BaseException("批量单据中有单据正在审核或已审核！");
            }
            //获取流程实例id
            String processInstanceId = processInstance.getId();
            List<Task> taskList = actFlowCommService.myTaskList(userName);
            if (!CollectionUtils.isEmpty(taskList)) {
                for (Task task : taskList) {
                    if (task.getAssignee().equals(userName) &&
                            task.getProcessInstanceId().equals(processInstanceId)) {
                        log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                        log.info("taskid is {}",task.getId());
                        actFlowCommService.completeProcess("同意", userName, task.getId(),"");
                    }
                }
            }
            else {
                throw new BaseException("空任务！");
            }
        });
        return Result.success("批量上报采购单成功");
    }
    @PostMapping("/getCurStatus")
    @ApiOperation("获得当前单据的审核状态")
    public Result getStatus(@RequestParam String TC001,@RequestParam String TC002){
        String formKey="purtc";
        String businessKey=formKey+":"+TC001+":"+TC002;
        log.info("businessKey={}",businessKey);
        List<TaskInfo> curProcessState = actFlowCommService.getCurProcessState(businessKey, formKey);
        return Result.success(curProcessState);
    }
    /*@PostMapping("/getCurStatus2")
    @ApiOperation("获得当前单据的审核状态2")
    public Result getStatus2(@RequestParam String TC001,@RequestParam String TC002){
        String formKey="purtc";
        String businessKey=formKey+":"+TC001+":"+TC002;
        actFlowCommService.getCurProcessState2(businessKey,formKey);
        return Result.success();
    }*/
    /*@RequestMapping(value = "/getFlowImgByInstanceId")
    @ApiOperation(value = "获取流程实例跟踪", httpMethod = "POST" ,notes = "获取流程实例跟踪")
    public void getFlowImgByInstanceId(@RequestParam String TC001,@RequestParam String TC002, HttpServletResponse response) {
        String formKey="purtc";
        String businessKey=formKey+":"+TC001+":"+TC002;
        actFlowCommService.getFlowImgByInstanceId(businessKey,formKey,response);
    }*/
    @Log("撤审采购单")
    @ApiOperation("撤审采购单")
    @PostMapping("/withdraw")
    public Result withdraw(@RequestParam(defaultValue = "") String TC001,
                                @RequestParam(defaultValue = "") String TC002) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        if (purtc == null) {
            return Result.error("采购单不存在");
        }
        ShenHe shenHe = new ShenHe();
        shenHe.setCom(YmlConfig.getYFCOMPANY());
        shenHe.setUser("DS");
        shenHe.setRq(TimeUtils.HZStdTime());
        shenHe.setType("UNDO");
        shenHe.setDb(TC001);
        shenHe.setDh(TC002);
        shenHe.setProgram("PURI07");

        HttpResult httpResult = HttpClientUtil.postHZ(shenHe);
        if (httpResult.getStatus().equals("成功")) {
            Purtc purtc1 = new Purtc();
            purtc1.setTc030("0");
            purtcMapper.update(purtc1, new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
            return Result.success("撤审成功");
        }
        return Result.success("撤审失败");
    }

    @Log("作废采购单")
    @ApiOperation("作废采购单")
    @PostMapping("/invalidate")
    public Result invalidate(@RequestParam(defaultValue = "") String TC001,
                             @RequestParam(defaultValue = "") String TC002) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Purtc purtc = purtcMapper.selectOne(new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
        if (purtc == null) {
            return Result.error("采购单不存在");
        }
        ShenHe shenHe = new ShenHe();
        shenHe.setCom(YmlConfig.getYFCOMPANY());
        shenHe.setUser("DS");
        shenHe.setRq(TimeUtils.HZStdTime());
        shenHe.setType("UNDO");
        shenHe.setDb(TC001);
        shenHe.setDh(TC002);
        shenHe.setProgram("PURI07");

        HttpResult httpResult = HttpClientUtil.postHZ(shenHe);
        if (httpResult.getStatus().equals("成功")) {
            Purtc purtc1 = new Purtc();
            purtc1.setTc030("0");
            purtc1.setTc014("V");
            purtcMapper.update(purtc1, new LambdaQueryWrapper<Purtc>().eq(Purtc::getTc001, TC001).eq(Purtc::getTc002, TC002));
            Purtd purtd=new Purtd();
            purtd.setTd018("V");
            purtdMapper.update(purtd,new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd001,TC001).eq(Purtd::getTd002,TC002));
            return Result.success("作废成功");
        }
        return Result.success("作废失败");
    }

}
